﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public string GetSaving(string sc, string mc, string sl, string nos)
        {
            
            double serverCost = double.Parse(sc); 
            double maintCost = double.Parse(mc);
            double softLicn = double.Parse(sl); 

            int noOfServers = Int32.Parse(nos);
            
            CloudEntities db = new CloudEntities();

            // instance size corelates with price of server
            string instanceSizeRequired;

            // set the cost/instance size relationship
            double vSmall = 500.00;
            double small = 750.00;
            double medium = 1000.00;
            double large = 1250.00;

            if (serverCost <= vSmall)
            {
                instanceSizeRequired = "Very Small";
            }
            else if ((serverCost > vSmall) && (serverCost <= small))
            {
                instanceSizeRequired = "Small";
            }
            else if ((serverCost > small) && (serverCost <= medium))
            {
                instanceSizeRequired = "Medium";
            }
            else if ((serverCost > medium) && (serverCost <= large))
            {
                instanceSizeRequired = "Large";
            }
            else
            {
                instanceSizeRequired = "Very Large";
            }

            // Query database to get cloud services of the size calculated above
            List<CloudService> services = db.CloudServices.Where(cs => cs.InstanceSize == instanceSizeRequired).OrderBy(cs => cs.MonthlyCost).ToList();

            decimal? cheapest = decimal.MaxValue;

            foreach (var c in services)
            {
                if (c.MonthlyCost <= cheapest)
                {
                    cheapest = c.MonthlyCost;
                }
            }

            CloudService service = db.CloudServices.Where(s => s.MonthlyCost == cheapest).Single();

            int yearsOfUse = 3;
            int months = 12;
            
            // Get Current Server Monthly Cost
            double serverMonthlyCost = serverCost / yearsOfUse / months;
            double monthlyCost = Math.Round((serverMonthlyCost + maintCost + softLicn) * noOfServers, 2);
                      
            // Calculate the saving

            double monthlySaving = monthlyCost - service.TotalCost;
            string output;
            
            if (monthlyCost > service.TotalCost)
            {
                output = "You could save: €" + Math.Round(monthlySaving, 2) + " per month, From: " + service.Company;
            }
            else if (service.TotalCost > monthlyCost)
            {
                output = "You could lose: €" + (Math.Round(monthlySaving, 2) * -1) + " per month by using a Cloud Service";
            }
            else
            {
                output = "There is no saving to be made";
            }

            return output;
            

            
        }
        
    }
}
